/*
   Copyright (c) 2011, 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *  Marco Riccardi 
 *	2011-02-18 - initial implementation
 *  2011-09-14 - changed Property -> AbstractProperty
 *  2015-01-11 - 5.22 MML
 *
 */
package org.mizartools.dli.utility;

import java.util.LinkedList;

import org.mizartools.dli.Abbreviation;
import org.mizartools.dli.Adjective;
import org.mizartools.dli.And;
import org.mizartools.dli.ArticleId;
import org.mizartools.dli.Constructor;
import org.mizartools.dli.DliException;
import org.mizartools.dli.Fields;
import org.mizartools.dli.FlexFrm;
import org.mizartools.dli.For;
import org.mizartools.dli.Format;
import org.mizartools.dli.Formula;
import org.mizartools.dli.Fraenkel;
import org.mizartools.dli.Func;
import org.mizartools.dli.Is;
import org.mizartools.dli.ItemId;
import org.mizartools.dli.ItemType;
import org.mizartools.dli.Loci;
import org.mizartools.dli.Locus;
import org.mizartools.dli.LocusVar;
import org.mizartools.dli.Not;
import org.mizartools.dli.Num;
import org.mizartools.dli.Part;
import org.mizartools.dli.Pred;
import org.mizartools.dli.Prefices;
import org.mizartools.dli.PrivateFormula;
import org.mizartools.dli.PrivateFunctor;
import org.mizartools.dli.ProperDefiniens;
import org.mizartools.dli.Properties;
import org.mizartools.dli.PropertyEnum;
import org.mizartools.dli.Redefinition;
import org.mizartools.dli.SymbolId;
import org.mizartools.dli.SymbolType;
import org.mizartools.dli.Term;
import org.mizartools.dli.The;
import org.mizartools.dli.Type;
import org.mizartools.dli.Var;
import org.mizartools.dli.Variable;
import org.mizartools.dli.Verum;
import org.mizartools.dli.Visible;
import org.mizartools.system.Symbols;
import org.mizartools.system.VocabularyFile;
import org.mizartools.system.utility.AbstractSignature;
import org.mizartools.system.utility.NotationsVocabulary;
import org.mizartools.system.utility.UniqueIdentifier;
import org.mizartools.system.utility.UniqueIdentifierException;
import org.mizartools.system.utility.VocabularyItem;
import org.mizartools.system.xml.ArgTypes;
import org.mizartools.system.xml.Pred.Kind;

class Adapter {

	static Fields getFields(
			org.mizartools.system.utility.AbstractSignature abstractSignature,
			org.mizartools.system.xml.Fields fields) throws DliException {
		LinkedList<ItemId> itemIdList = new LinkedList<ItemId>();
		for (org.mizartools.system.xml.Field field : fields.getFieldList()) {
			ItemId itemId = getItemId(abstractSignature, field);
			itemIdList.add(itemId);
		}
		Fields fieldsDli = new Fields(itemIdList);
		return fieldsDli;
	}

	private static ItemId getItemId(
			org.mizartools.system.utility.AbstractSignature abstractSignature, 
			org.mizartools.system.xml.Field field) throws DliException {
		UniqueIdentifier uniqueIdentifier = null;
		try {
			uniqueIdentifier = UniqueIdentifier.getInstance(abstractSignature, field);
		} catch (UniqueIdentifierException e) {
			throw new DliException();
		}
		ArticleId articleId = new ArticleId(uniqueIdentifier.aid);
		ItemType itemType = null;
		switch (uniqueIdentifier.type){
		case mode : itemType = ItemType.mode; break;
		case struct : itemType = ItemType.struct; break;
		case sel : itemType = ItemType.sel; break;
		default : throw new DliException();
		}
		ItemId itemId = new ItemId(articleId, itemType, uniqueIdentifier.nr);
		return itemId;
	}

	static Prefices getPrefices(
			org.mizartools.system.utility.AbstractSignature abstractSignature, 
			LinkedList<org.mizartools.system.xml.Typ> typList) throws DliException {
		LinkedList<Type> typeList = new LinkedList<Type>();
		for (org.mizartools.system.xml.Typ typ : typList) {
			Type type = getType(abstractSignature, typ);
			typeList.add(type);
		}
		Prefices prefices = new Prefices(typeList);
		return prefices;
	}

	static Redefinition getRedefinition(
			org.mizartools.system.utility.AbstractSignature abstractSignature, 
			org.mizartools.system.xml.Constructor constructor) throws DliException {
		if (constructor.getRedefnr() == null) return null;
		LinkedList<Adjective> adjectiveList = new LinkedList<Adjective>();
		LinkedList<Term> termList = new LinkedList<Term>();
		UniqueIdentifier uniqueIdentifier = null;
		try {
			uniqueIdentifier = UniqueIdentifier.getInstance(abstractSignature, constructor.getRedefaid() , constructor.getKind(), constructor.getRedefnr());
		} catch (UniqueIdentifierException e) {
			throw new DliException();
		}
		ArticleId articleId = new ArticleId(uniqueIdentifier.aid);
		ItemType itemType = null;
		switch (uniqueIdentifier.type){
		case mode : itemType = ItemType.mode; break;
		case func : itemType = ItemType.func; break;
		case pred : itemType = ItemType.pred; break;
		case attr : itemType = ItemType.attr; break;
		default : throw new DliException();
		}
		ItemId itemId = new ItemId(articleId, itemType, uniqueIdentifier.nr);
		Type type = new Type(adjectiveList, itemId, termList, false);
		Redefinition redefinition = new Redefinition(type);
		return redefinition;
	}

	static Type getType(
			org.mizartools.system.utility.AbstractSignature abstractSignature,
			org.mizartools.system.xml.Typ typ) throws DliException{
		    LinkedList<Adjective> adjectiveList = getAdjectiveList(abstractSignature, typ.getClusterList());
			ItemId itemId = getItemId(abstractSignature, typ);
			VariableId vid = new VariableId();
		    LinkedList<Term> termList = getTermList(abstractSignature, typ.getTermList(), vid);
			Type type = new Type(adjectiveList, itemId, termList, true);
			return type;
		}

	static Properties getProperties(
			org.mizartools.system.xml.Properties properties) throws DliException {
		if (properties == null) return null;
	    LinkedList<PropertyEnum> propertyEnumList = new LinkedList<PropertyEnum>();
	    for (org.mizartools.system.xml.AbstractProperty abstractProperty : properties.getAbstractPropertyList()){
	    	PropertyEnum propertyEnum = PropertyEnum.valueOf(abstractProperty.getPropertyName().toLowerCase());
	    	propertyEnumList.add(propertyEnum);
	    }
		Properties propertiesDli = new Properties(propertyEnumList);
		return propertiesDli;
	}

	static LinkedList<Locus> getLocusList(
			org.mizartools.system.utility.AbstractSignature abstractSignature, 
			org.mizartools.system.xml.ArgTypes argTypes) 
	throws DliException {
		LinkedList<Locus> locusList = new LinkedList<Locus>();
		int nr = 0;
		for (org.mizartools.system.xml.Typ typ : argTypes.getTypList()) {
			nr++;
			LocusVar locusVar = new LocusVar(nr);
			Type type = getType(abstractSignature, typ);
			locusList.add(new Locus(locusVar, type));
		}
		return locusList;
	} 

	private static LinkedList<Term> getTermList(
			org.mizartools.system.utility.AbstractSignature abstractSignature,
			LinkedList<org.mizartools.system.xml.Term> termList,
			VariableId vid) throws DliException {
	    LinkedList<Term> termDliList = new LinkedList<Term>();
	    for (org.mizartools.system.xml.Term term : termList){
	    	// FreeVar, LambdaVar, Const, InfConst, Num, PrivFunc, Fraenkel, Choice, QuaTrm, It
	    	Term termDli;
	        if (term instanceof org.mizartools.system.xml.Func) {
				org.mizartools.system.xml.Func func = (org.mizartools.system.xml.Func) term;
	    		LinkedList<Term> term2List = getTermList(abstractSignature, ((org.mizartools.system.xml.Func)term).getTermList(),vid);
				if (func.getKind() == org.mizartools.system.xml.Func.Kind.F){
					if (func.getNr() == null) throw new DliException();
					termDli = new PrivateFunctor(func.getNr(), term2List);
				} else {
		    		ItemId itemId = getItemId(abstractSignature, ((org.mizartools.system.xml.Func)term));	
		        	termDli = new Func(itemId, term2List);
				}
        	} else if (term instanceof org.mizartools.system.xml.Var) {
	        	termDli = new Variable(((org.mizartools.system.xml.Var)term).getNr());
        	} else if (term instanceof org.mizartools.system.xml.LocusVar) {
	        	termDli = new LocusVar(((org.mizartools.system.xml.LocusVar)term).getNr());
        	} else if (term instanceof org.mizartools.system.xml.Num) {
	        	termDli = new Num(((org.mizartools.system.xml.Num)term).getNr());
        	} else if (term instanceof org.mizartools.system.xml.Fraenkel) {
        		org.mizartools.system.xml.Fraenkel fraenkel = (org.mizartools.system.xml.Fraenkel)term;
    			LinkedList<Var> varList = new LinkedList<Var>();
    			VariableId vid2 = new VariableId();
    			vid2.setId(vid.getId());
    			for (org.mizartools.system.xml.Typ typ : fraenkel.getTypList()) {
    				vid2.increment();
    				Variable variable = new Variable(vid2.getId());
    				Type type = getType(abstractSignature, typ);
    				varList.add(new Var(variable, type));
    			}
    			Term term1 = getTerm(abstractSignature, fraenkel.getTerm(), vid2);
    			termDli = new Fraenkel(varList, term1, getFormula(abstractSignature, fraenkel.getFormula(), vid2));
        	} else {
    			throw new DliException();
        	}
	        termDliList.add(termDli);
	    }
	    return termDliList;
	} 

	private static ItemId getItemId(
			org.mizartools.system.utility.AbstractSignature abstractSignature,
			org.mizartools.system.xml.Func func) 
		throws DliException {
			UniqueIdentifier uniqueIdentifier = null;
			try {
				uniqueIdentifier = UniqueIdentifier.getInstance(abstractSignature, func);
			} catch (UniqueIdentifierException e) {
				throw new DliException();
			}
			ArticleId articleId = new ArticleId(uniqueIdentifier.aid);
			ItemType itemType = null;
			switch (uniqueIdentifier.type){
			case mode : itemType = ItemType.mode; break;
			case func : itemType = ItemType.func; break;
			case struct : itemType = ItemType.struct; break;
			case sel : itemType = ItemType.sel; break;
			case aggr : itemType = ItemType.aggr; break;
			default : throw new DliException();
			}
			ItemId itemId = new ItemId(articleId, itemType, uniqueIdentifier.nr);
			return itemId;
		}

	private static ItemId getItemId(
			org.mizartools.system.utility.AbstractSignature abstractSignature, 
			org.mizartools.system.xml.Typ typ) 
	throws DliException {
		UniqueIdentifier uniqueIdentifier = null;
		try {
			uniqueIdentifier = UniqueIdentifier.getInstance(abstractSignature, typ);
		} catch (UniqueIdentifierException e) {
			throw new DliException();
		}
		ArticleId articleId = new ArticleId(uniqueIdentifier.aid);
		ItemType itemType = null;
		switch (uniqueIdentifier.type){
		case mode : itemType = ItemType.mode; break;
		case struct : itemType = ItemType.struct; break;
		default : throw new DliException();
		}
		ItemId itemId = new ItemId(articleId, itemType, uniqueIdentifier.nr);
		return itemId;
	}

	static LinkedList<Adjective> getAdjectiveList(
			org.mizartools.system.utility.AbstractSignature abstractSignature,
			LinkedList<org.mizartools.system.xml.Cluster> clusterList) throws DliException{
	    LinkedList<Adjective> adjectiveList = new LinkedList<Adjective>();
	    for (org.mizartools.system.xml.Cluster cluster : clusterList){
	    	for (org.mizartools.system.xml.Adjective adjective : cluster.getAdjectiveList()){
	    		Boolean value = adjective.getValue();
	    		if (value == null) value = true;
	    		ItemId itemId = getItemId(abstractSignature, adjective);
	    		VariableId vid = new VariableId();
	    		LinkedList<Term> termList = getTermList(abstractSignature, adjective.getTermList(),vid);
		    	Adjective adjectiveDli = new Adjective(value, itemId, termList);
		    	adjectiveList.add(adjectiveDli);
	    	}
	    }
	    return adjectiveList;
	} 

	private static ItemId getItemId(
			org.mizartools.system.utility.AbstractSignature abstractSignature, 
			org.mizartools.system.xml.Adjective adjective) 
	throws DliException {
		UniqueIdentifier uniqueIdentifier = null;
		try {
			uniqueIdentifier = UniqueIdentifier.getInstance(abstractSignature, adjective);
		} catch (UniqueIdentifierException e) {
			throw new DliException();
		}
		ArticleId articleId = new ArticleId(uniqueIdentifier.aid);
		ItemType itemType = null;
		switch (uniqueIdentifier.type){
		case mode : itemType = ItemType.mode; break;
		case attr : itemType = ItemType.attr; break;
		default : throw new DliException();
		}
		ItemId itemId = new ItemId(articleId, itemType, uniqueIdentifier.nr);
		return itemId;
	}

	static Abbreviation getAbbreviation(
			org.mizartools.system.utility.AbstractSignature abstractSignature, 
			org.mizartools.system.xml.Expansion expansion) throws DliException {
		if (expansion == null) return null;
		Type type = getType(abstractSignature, expansion.getTyp());
		Abbreviation abbreviation = new Abbreviation(type); 
		return abbreviation;
	}

	static Constructor getConstructor(
			org.mizartools.system.utility.AbstractSignature abstractSignature, 
			org.mizartools.system.xml.Pattern pattern) 
		throws DliException {
		if (pattern.getConstrnr() == 0) return null;
		UniqueIdentifier uniqueIdentifier = null;
		try {
			uniqueIdentifier = UniqueIdentifier.getInstance(abstractSignature, null, pattern.getConstrkind(), pattern.getConstrnr());
		} catch (UniqueIdentifierException e) {
			throw new DliException();
		}
		ArticleId articleId = new ArticleId(uniqueIdentifier.aid);
		ItemType itemType = null;
		switch (uniqueIdentifier.type){
		case mode : itemType = ItemType.mode; break;
		case attr : itemType = ItemType.attr; break;
		case pred : itemType = ItemType.pred; break;
		case func : itemType = ItemType.func; break;
		case struct : itemType = ItemType.struct; break;
		case sel : itemType = ItemType.sel; break;
		case aggr : itemType = ItemType.aggr; break;
		case forg : itemType = ItemType.forg; break;
		default : throw new DliException();
		}
		ItemId itemId = new ItemId(articleId, itemType, uniqueIdentifier.nr);
		Constructor constructor = new Constructor(itemId);
		return constructor;
	}

	static SymbolId getSymbolId1(
			org.mizartools.system.xml.Pattern pattern) 
		throws DliException {
		int symbolNr = pattern.getFormat().getSymbolnr();
		NotationsVocabulary notationsVocabulary = NotationsVocabulary.getNotationsVocabulary(pattern.getAid());
		String aid = null;
		if (aid == null){
			for (VocabularyItem vocabularyItem : notationsVocabulary.getVocabularyItemList()){
				org.mizartools.system.xml.Format.Kind kind = pattern.getFormat().getKind(); 
				switch (kind){
				case M : 
					if (symbolNr > vocabularyItem.nrM) symbolNr = symbolNr - vocabularyItem.nrM;
					else aid = vocabularyItem.articleId;
					break;
				case G : 
					if (symbolNr > vocabularyItem.nrG) symbolNr = symbolNr - vocabularyItem.nrG;
					else aid = vocabularyItem.articleId;
					break;
				case L : 
					if (symbolNr > vocabularyItem.nrG) symbolNr = symbolNr - vocabularyItem.nrG;
					else aid = vocabularyItem.articleId;
					break;
				case R :
					if (symbolNr > vocabularyItem.nrR) symbolNr = symbolNr - vocabularyItem.nrR;
					else aid = vocabularyItem.articleId;
					break;
				case K :
					if (symbolNr > vocabularyItem.nrK) symbolNr = symbolNr - vocabularyItem.nrK;
					else aid = vocabularyItem.articleId;
					break;
				case U :
					if (symbolNr > vocabularyItem.nrU) symbolNr = symbolNr - vocabularyItem.nrU;
					else aid = vocabularyItem.articleId;
					break;
				case V :
					if (symbolNr > vocabularyItem.nrV) symbolNr = symbolNr - vocabularyItem.nrV;
					else aid = vocabularyItem.articleId;
					break;
				case O :
					if (symbolNr > vocabularyItem.nrO) symbolNr = symbolNr - vocabularyItem.nrO;
					else aid = vocabularyItem.articleId;
					break;
				case J :
					if (symbolNr > vocabularyItem.nrG) symbolNr = symbolNr - vocabularyItem.nrG;
					else aid = vocabularyItem.articleId;
					break;
				default :
					throw new DliException();
				}
				if (aid != null) break;
			}
		}
		if (aid == null) aid = pattern.getAid();
		Symbols symbols = VocabularyFile.getSymbols(aid);
		SymbolId symbolId = null;
		SymbolType symbolType = null;
		ArticleId articleId = new ArticleId(aid);
		String token = null;
		org.mizartools.system.xml.Format.Kind kind = pattern.getFormat().getKind(); 
		switch (kind){
		case O : 
			symbolType = SymbolType.vocO;
			token = symbols.getSymbolO(symbolNr);
			break;
		case R : 
			symbolType = SymbolType.vocR;
			token = symbols.getSymbolR(symbolNr);
			break;
		case V :
			symbolType = SymbolType.vocV;
			token = symbols.getSymbolV(symbolNr);
			break;
		case M :
			symbolType = SymbolType.vocM;
			token = symbols.getSymbolM(symbolNr);
			break;
		case K :
			symbolType = SymbolType.vocK;
			token = symbols.getSymbolK(symbolNr);
			break;
		case L :
			symbolType = SymbolType.vocG;
			token = symbols.getSymbolG(symbolNr);
			break;
		case G :
			symbolType = SymbolType.vocG;
			token = symbols.getSymbolG(symbolNr);
			break;
		case U :
			symbolType = SymbolType.vocU;
			token = symbols.getSymbolU(symbolNr);
			break;
		case J :
			symbolType = SymbolType.vocG;
			token = symbols.getSymbolG(symbolNr);
			break;
		default :
			throw new DliException();
		}
		symbolId = new SymbolId(articleId, symbolType, token, null, symbolNr);
		return symbolId;
	}

	static SymbolId getSymbolId2(
			org.mizartools.system.xml.Pattern pattern) 
		throws DliException {
		org.mizartools.system.xml.Format.Kind kind = pattern.getFormat().getKind();
		if (kind != org.mizartools.system.xml.Format.Kind.K) return null;
		kind = org.mizartools.system.xml.Format.Kind.L;
		int symbolNr = pattern.getFormat().getRightsymbolnr();
		NotationsVocabulary notationsVocabulary = NotationsVocabulary.getNotationsVocabulary(pattern.getAid());
		String aid = null;
		if (aid == null){
			for (VocabularyItem vocabularyItem : notationsVocabulary.getVocabularyItemList()){
				if (symbolNr > vocabularyItem.nrL) symbolNr = symbolNr - vocabularyItem.nrL;
				else aid = vocabularyItem.articleId;
				if (aid != null) break;
			}
		}
		if (aid == null) aid = pattern.getAid();
		Symbols symbols = VocabularyFile.getSymbols(aid);
		SymbolId symbolId = null;
		SymbolType symbolType = null;
		ArticleId articleId = new ArticleId(aid);
		String token = null;
		symbolType = SymbolType.vocL;
		token = symbols.getSymbolL(symbolNr);
		symbolId = new SymbolId(articleId, symbolType, token, null, symbolNr);
		return symbolId;
	}
	
	
	static Format getFormat(org.mizartools.system.xml.Format format) throws DliException {
		Integer nrLeft = format.getLeftargnr();
		Integer nrTot = format.getArgnr();
		Integer nrRight = 0;
		if (nrLeft == null) {
			nrLeft = 0;
			nrRight = nrTot;}
		else {
			nrRight = nrTot - nrLeft;
		}
		LinkedList<Integer> integerList = new LinkedList<Integer>();
		integerList.add(nrLeft);
		integerList.add(nrRight);
		Format formatDli = new Format(integerList);
		return formatDli;
	} 

	static Visible getVisible(org.mizartools.system.xml.Visible visible) throws DliException {
		LinkedList<Integer> integerList = new LinkedList<Integer>();
		for (org.mizartools.system.xml.Int int1 : visible.getIntList()){
			integerList.add(int1.getX());
		}
		Visible visibleDli = new Visible(integerList);
		return visibleDli;
	}

	static ProperDefiniens getProperDefiniens(
			AbstractSignature abstractSignature, 
			org.mizartools.system.xml.DefMeaning defMeaning,
			VariableId vid) 
	throws DliException {
		LinkedList<Part> partList = new LinkedList<Part>();
		VariableId vid2 = new VariableId();
		vid2.setId(vid.getId());
		for (org.mizartools.system.xml.PartialDef partialDef : defMeaning.getPartialDefList()){
			partList.add(getPart(abstractSignature, partialDef, vid2));
		}
		Formula formula = getFormula(abstractSignature, defMeaning.getFormula(), vid2);
		Term term = getTerm(abstractSignature, defMeaning.getTerm(), vid2);
		ProperDefiniens properDefiniens = new ProperDefiniens(partList , formula, term);
		return properDefiniens;
	}

	private static Part getPart(AbstractSignature abstractSignature,
			org.mizartools.system.xml.PartialDef partialDef,
			VariableId vid) throws DliException {
		VariableId vid1 = new VariableId();
		vid1.setId(vid.getId());
		VariableId vid2 = new VariableId();
		vid2.setId(vid.getId());
		VariableId vid3 = new VariableId();
		vid3.setId(vid.getId());
		Part part = new Part(
				getFormula(abstractSignature, partialDef.getFormula1(), vid1),
				getTerm(abstractSignature, partialDef.getTerm(), vid2), 
				getFormula(abstractSignature, partialDef.getFormula2(), vid3));
		return part;
	}


	static Term getTerm(AbstractSignature abstractSignature,
			org.mizartools.system.xml.Term term,
			VariableId vid) throws DliException {
		if (term == null) return null;
		Term termDli = null;
		if (term instanceof org.mizartools.system.xml.Var) {
			termDli = new Variable(((org.mizartools.system.xml.Var)term).getNr());
		} else if (term instanceof org.mizartools.system.xml.LocusVar) {
			termDli = new LocusVar(((org.mizartools.system.xml.LocusVar)term).getNr());
		} else if (term instanceof org.mizartools.system.xml.Func) {
			org.mizartools.system.xml.Func func = (org.mizartools.system.xml.Func) term;
			LinkedList<Term> termList = new LinkedList<Term>();
			for (org.mizartools.system.xml.Term term1 : func.getTermList()) {
				termList.add(getTerm(abstractSignature, term1, vid));
			}
			if (func.getKind() == org.mizartools.system.xml.Func.Kind.F){
				if (func.getNr() == null) throw new DliException();
				termDli = new PrivateFunctor(func.getNr(), termList);
			} else {
				ItemId itemId = getItemId(abstractSignature, func.getKind(), func.getNr());
				termDli = new Func(itemId, termList);
			}
		} else if (term instanceof org.mizartools.system.xml.Choice) {
			termDli = new The(getType(abstractSignature, ((org.mizartools.system.xml.Choice)term).getTyp()));
		} else if (term instanceof org.mizartools.system.xml.Num) {
			termDli = new Num(((org.mizartools.system.xml.Num)term).getNr());
		} else if (term instanceof org.mizartools.system.xml.Fraenkel) {
			org.mizartools.system.xml.Fraenkel fraenkel = (org.mizartools.system.xml.Fraenkel)term;
			LinkedList<Var> varList = new LinkedList<Var>();
			VariableId vid2 = new VariableId();
			vid2.setId(vid.getId());
			for (org.mizartools.system.xml.Typ typ : fraenkel.getTypList()) {
				vid2.increment();
				Variable variable = new Variable(vid2.getId());
				Type type = getType(abstractSignature, typ);
				varList.add(new Var(variable, type));
			}
			Term term1 = getTerm(abstractSignature, fraenkel.getTerm(), vid2);
			termDli = new Fraenkel(varList, term1, getFormula(abstractSignature, fraenkel.getFormula(), vid2));
		} else throw new DliException();
		return termDli;
	}

	private static ItemId getItemId(
			AbstractSignature abstractSignature,
			org.mizartools.system.xml.Func.Kind kind, Integer nr) throws DliException {
		UniqueIdentifier uniqueIdentifier = null;
		try {
			uniqueIdentifier = UniqueIdentifier.getInstance(abstractSignature, null, kind, nr);
		} catch (UniqueIdentifierException e) {
			throw new DliException();
		}
		ArticleId articleId = new ArticleId(uniqueIdentifier.aid);
		ItemType itemType = null;
		switch (uniqueIdentifier.type){
		case func : itemType = ItemType.func; break;
		case struct : itemType = ItemType.struct; break;
		case sel : itemType = ItemType.sel; break;
		case aggr : itemType = ItemType.aggr; break;
		default : throw new DliException();
		}
		ItemId itemId = new ItemId(articleId, itemType, uniqueIdentifier.nr);
		return itemId;
	}

	static Formula getFormula(AbstractSignature abstractSignature,
			org.mizartools.system.xml.Formula formula,
			VariableId vid) throws DliException {
		if (formula == null) return null;
		Formula formulaDli = null;
		if (formula instanceof org.mizartools.system.xml.For){
			Integer vid1 = ((org.mizartools.system.xml.For) formula).getVid();
			if (vid1 == null) {
				vid.increment();
				vid1 = vid.getId();
			}
			Variable variable = new Variable(vid1);
			Type type = getType(abstractSignature, ((org.mizartools.system.xml.For) formula).getTyp());
			VariableId vid2 = new VariableId();
			vid2.setId(vid.getId());
			Formula formula1 = getFormula(abstractSignature, ((org.mizartools.system.xml.For) formula).getFormula(), vid2);
			formulaDli = new For(variable, type, formula1);
		} else if (formula instanceof org.mizartools.system.xml.And){
		    LinkedList<Formula> formulaList = new LinkedList<Formula>();
			for (org.mizartools.system.xml.Formula formula1 : ((org.mizartools.system.xml.And) formula).getFormulaList()) {
				VariableId vid2 = new VariableId();
				vid2.setId(vid.getId());
				formulaList.add(getFormula(abstractSignature, formula1, vid2));
			}
			formulaDli = new And(formulaList);
		} else if (formula instanceof org.mizartools.system.xml.Not){
			formulaDli = new Not(getFormula(abstractSignature, ((org.mizartools.system.xml.Not) formula).getFormula(), vid));
		} else if (formula instanceof org.mizartools.system.xml.Pred){
			org.mizartools.system.xml.Pred pred = (org.mizartools.system.xml.Pred) formula;
			LinkedList<Term> termList = new LinkedList<Term>();
			for (org.mizartools.system.xml.Term term : pred.getTermList()) {
				termList.add(getTerm(abstractSignature, term, vid));
			}
			if (pred.getKind() == Kind.P) {
				if (pred.getNr() == null) throw new DliException();
				formulaDli = new PrivateFormula(pred.getNr(), termList);
			}
			else {
				ItemId itemId = getItemId(abstractSignature, pred.getKind(), pred.getNr());
				switch (pred.getKind()){
				case V :
					LinkedList<Adjective> adjectiveList = new LinkedList<Adjective>();
					Type type = new Type(adjectiveList, itemId, termList, false);
					formulaDli = new Is(termList, type);
					break;
				default :
					formulaDli = new Pred(itemId, termList);
				}
			}
		} else if (formula instanceof org.mizartools.system.xml.Is){
			org.mizartools.system.xml.Is is = (org.mizartools.system.xml.Is) formula;
			Type type = getType(abstractSignature, is.getTyp());
			LinkedList<Term> termList = new LinkedList<Term>();
			termList.add(getTerm(abstractSignature, is.getTerm(), vid));
			formulaDli = new Is(termList, type);
		} else if (formula instanceof org.mizartools.system.xml.Verum){
			formulaDli = new Verum();
		} else if (formula instanceof org.mizartools.system.xml.FlexFrm){
			formulaDli = new FlexFrm();
		} else throw new DliException();
		return formulaDli;
	}

	
	private static ItemId getItemId(
			AbstractSignature abstractSignature, 
			org.mizartools.system.xml.Pred.Kind kind, Integer nr) throws DliException {
		UniqueIdentifier uniqueIdentifier = null;
		try {
			uniqueIdentifier = UniqueIdentifier.getInstance(abstractSignature, null, kind, nr);
		} catch (UniqueIdentifierException e) {
			throw new DliException();
		}
		ArticleId articleId = new ArticleId(uniqueIdentifier.aid);
		ItemType itemType = null;
		switch (uniqueIdentifier.type){
		case attr : itemType = ItemType.attr; break;
		case pred : itemType = ItemType.pred; break;
		default : throw new DliException();
		}
		ItemId itemId = new ItemId(articleId, itemType, uniqueIdentifier.nr);
		return itemId;
	}

	static Visible getVisible(
			org.mizartools.system.xml.Essentials essentials) throws DliException {
		LinkedList<Integer> integerList = new LinkedList<Integer>();
		for (org.mizartools.system.xml.Int int1 : essentials.getIntList()){
			integerList.add(int1.getX());
		}
		Visible visible = new Visible(integerList);
		return visible;
	}

	static ItemId getItemId(
			AbstractSignature abstractSignature, 
			org.mizartools.system.xml.Definiens.Constrkind constrkind,
			Integer constrnr) throws DliException {
		UniqueIdentifier uniqueIdentifier = null;
		try {
			uniqueIdentifier = UniqueIdentifier.getInstance(abstractSignature, null, constrkind, constrnr);
		} catch (UniqueIdentifierException e) {
			throw new DliException();
		}
		ArticleId articleId = new ArticleId(uniqueIdentifier.aid);
		ItemType itemType = null;
		switch (uniqueIdentifier.type){
		case mode : itemType = ItemType.mode; break;
		case attr : itemType = ItemType.attr; break;
		case aggr : itemType = ItemType.aggr; break;
		case func : itemType = ItemType.func; break;
		case pred : itemType = ItemType.pred; break;
		default : throw new DliException();
		}
		ItemId itemId = new ItemId(articleId, itemType, uniqueIdentifier.nr);
		return itemId;
	}

	static Loci getLoci(AbstractSignature abstractSignature,
			LinkedList<org.mizartools.system.xml.Typ> typList) throws DliException {
	    LinkedList<Type> typeList = new LinkedList<Type>();
//		int nr = 0;
		for (org.mizartools.system.xml.Typ typ : typList) {
//			nr++;
			Type type = getType(abstractSignature, typ);
			typeList.add(type);
		}
		Loci loci = new Loci(typeList);
		return loci;
	}

	static Loci getLoci(
			AbstractSignature abstractSignature, 
			org.mizartools.system.xml.ArgTypes argTypes) 
	throws DliException {
		return getLoci(abstractSignature, argTypes.getTypList());
	}
	
	static LinkedList<Formula> getFormulaList(
			AbstractSignature abstractSignature,
			LinkedList<org.mizartools.system.xml.Formula> formulaList) throws DliException {
		LinkedList<Formula> formulaDliList = new LinkedList<Formula>();
		for (org.mizartools.system.xml.Formula formula : formulaList) {
			VariableId vid = new VariableId();
			formulaDliList.add(getFormula(abstractSignature, formula, vid));
		}
		return formulaDliList;
	}

	static LinkedList<Type> getParameters(
			AbstractSignature abstractSignature, 
			ArgTypes argTypes) throws DliException {
		LinkedList<Type> typeList = new LinkedList<Type>();
		for (org.mizartools.system.xml.Typ typ : argTypes.getTypList()) {
			typeList.add(getType(abstractSignature, typ));
		}
		return typeList;
	}

}
